#include"Slist.h"
void SLPrint(SLNode* phead)
{
	while (phead != NULL)
	{
		printf("%d->", phead->data);
		phead = phead->next;
	}
	printf("NULL\n");
}
SLDataTypedef* SLBuyNode(SLDataTypedef x)
{
	SLNode* newNode = (SLNode*)malloc(sizeof(SLNode));
	if (newNode == NULL)
	{
		perror("malloc");
		return 1;
	}
	newNode->data = x;
	newNode->next = NULL;
	return newNode;
}
void SLPushBack(SLNode** phead, SLDataTypedef x)
{
	SLNode* newNode = SLBuyNode(x);

	if (*phead == NULL)
	{
		*phead = newNode;
	}
	else
	{
		SLNode* ptail = *phead;
		while (ptail->next != NULL)
		{
			ptail = ptail->next;
		}
		ptail->next = newNode;
	}
}
void SLPushFront(SLNode** phead, SLDataTypedef x)
{
	SLNode* newNode = SLBuyNode(x);
	newNode->next = *phead;
	*phead = newNode;
}
//头删
void SLPopPushBack(SLNode** phead)
{
	
	if ((*phead)->next = NULL)
	{
		free(*phead);
		*phead = NULL;
	}
	else
	{
		SLNode* prev = *phead;
		SLNode* ptail = *phead;
		while (ptail->next != NULL)
		{
			prev = ptail;
			ptail = ptail->next;
		}
		free(ptail);
		ptail = NULL;
		prev->next = NULL;
	}
}
//尾删
void SLPopPushFront(SLNode** phead)
{
	SLNode* next = (*phead)->next;
	free(*phead);
	*phead = next;
}
//查找
SLNode* SLFind(SLNode* phead, SLDataTypedef x)
{
	SLNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data == x)
			return pcur;
		pcur = pcur->next;
	}
	return NULL;
}
//在指定位置之前插入数据
void SLInsert(SLNode** phead, SLNode* pos, SLDataTypedef x)
{
	SLNode* newNode = SLBuyNode(x);
	if (pos == *phead)
	{
		SLPopPushBack(*phead, x);
		return 1;
	}
	SLNode* prev = *phead;
	while (prev->next != pos)
	{
		prev = prev->next;
	}
	newNode->next = pos;
	prev->next = newNode;
}
//在指定位置之后插入数据
void SLInsertAfter(SLNode* pos, SLDataTypedef x)
{
	SLNode* newNode = SLBuyNode(x);
	newNode->next = pos->next;
	pos->next = newNode;
}
//删除节点
void SLErasr(SLNode** phead, SLNode* pos)
{
	SLNode* prev = *phead;
	while (prev->next != pos)
	{
		prev = prev->next;
	}
	prev->next = pos->next;
	free(pos);
	pos = NULL;
}
//删除pos之后的节点
void SLEraseAfter(SLNode* pos)
{
	SLNode* next = pos->next;
	assert(next->next);
	pos->next = next->next;
	free(next);
	next = NULL;
}
//销毁链表
void SListDesTroy(SLNode** phead)
{
	SLNode* pcur = *phead;
	while (pcur)
	{
		SLNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	*phead = NULL;
}
